﻿var currentOrgId = null;
var parentOrgId = null;
var treeInstance = null;
$(function () {
    //树初始化
    treeInstance = $('#treeDiv').tree({//tree为容器ID
        asyn: true,
        rootId: 'root',
        rootText: '数据字典',
        CommandName: 'java.tree.command',
        onNodeClick: leftClick,
        params: {
            sqlId: 'dataDictionary.tree',
            parentId: 'root'
        },
        contextMenu: function (record) {
            var items = [];

            if (record.id == 'root') {
                items.push({ text: "添加数据字典", icon: Global.contextPath + "/Images/icon_new.gif", id: record.id, action: addAction });
               
            } else {
                items.push({ text: "修改数据字典", icon: Global.contextPath + "/Images/ico_edit.gif", id: record.id, action: updateAction });
                items.push({ text: "删除数据字典", icon: Global.contextPath + "/Images/ico_del.gif", id: record.id, action: deleteAction });
            }

            return {
                width: 120,
                items: items
            };
        }
    });
});

function reloadTree(){
    treeInstance.reload();
    treeInstance.expandLevel(null, 2);
}

function leftClick(id, text, record, currentNode) {
    currentOrgId = id;
    var url = Global.contextPath + '/Modules/DataDictionary/DataDictionaryItemList.aspx';
    if (id) {//ID不存在，点击根节点
        if (id != 'root') {
            url += '?DictionaryId=' + id + '&DictionaryName=' + encodeURI(text);
            document.getElementById("content_frame").src = url;
        }
    }

    
}

function addAction() {
    var url = "DataDictionaryEdit.aspx";
    var params = { FormViewMode: "Insert" };
    params.title = "添加数据字典";

    var returnValue = showCenterModalDialog(buildUrl(url, params), 500, 300, null);

    reloadTree();
}

function updateAction() {
    var url = "DataDictionaryEdit.aspx";
    var params = { FormViewMode: "Edit" };
    params.title = "编辑数据字典";
    params.Id = this.data.id;

    var returnValue = showCenterModalDialog(buildUrl(url, params), 500, 300, null);
    reloadTree();
}

function deleteAction() {
    var dictionaryId = this.data.id;

    if (window.confirm("确认要删除该数据字典吗？")) {
        $.dataservice("DataDictionaryService$DeleteDataDictionary", { dictionaryId: dictionaryId }, function (response) {
            alert("删除数据字典成功！");
            reloadTree();
        });
    }
}

